/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.spmif;

import java.util.Iterator;

import org.mitre.xmltransform.XmlInputDocument;
import org.mitre.xmltransform.XmlOutputDocument;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ExecutionStep extends HierarchyStep {
	public ExecutionStep(String pId, String pExecId, Node pNode, String pNodeName, XmlInputDocument pInput, XmlOutputDocument pOutput) {
		super(pId, pExecId, pNode, pNodeName, pInput, pOutput);
	}
	
	public String getIdPrefix() {
		return "EXEC";
	}

	public String getStartNodeName() {
		String name = super.getStartNodeName();
		if (hasChildren()) {
			name += "-1";
		}
		return name;
	}
	
	public void generateStep(Element pParentElement, Device pLocation) {
		//mNodeName = buildEventOccurrenceNodeName(getNode());
		Device location = getLocation();
		if (location == null) location = pLocation;
		if (location == null) location = new Device("Default", Device.TYPE_PROCESSING, null, 1);
		Element basicNodeElem;
		if (hasChildren()) {
			Iterator<HierarchyStep> children = this.getChildren();
			String pieceName = getNodeName();
			int index = 1;
			basicNodeElem = mOutput.addElement(pParentElement, "BasicNode", null);				
			basicNodeElem.setAttribute("NodeName", pieceName + "-" + (index++));
			HierarchyStep child;
			while (children.hasNext()) {
				child = children.next();
				child.generateStep(pParentElement, location);
				basicNodeElem = mOutput.addElement(pParentElement, "BasicNode", null);				
				basicNodeElem.setAttribute("NodeName", pieceName + "-" + (index++));
			}
		} else {
			basicNodeElem = mOutput.addElement(pParentElement, "BasicNode", null);
			basicNodeElem.setAttribute("NodeName", getNodeName());
			basicNodeElem.setAttribute("NodeId", getId()); //mOutput.getNextId());
			
			if (getDuration() != null) {
				Element rr = mOutput.addElement(basicNodeElem, "ResourceRequirement", null);
				rr.setAttribute("ResourceId", "R" + location.getName());
				rr.setAttribute("UnitsOfService", getDuration());
			}
		}
	}
	
	public String generateArcs(Element pParentElement, String pFromNode) {
		String fromNode = pFromNode;
		Element arcNode;
		if (hasChildren()) {
			Iterator<HierarchyStep> children = this.getChildren();
			int index = 1;
			String toNode = getId() + "-" + (index++); //getNodeName() + "-" + (index++);
			if (fromNode != null) {
				arcNode = mOutput.addElement(pParentElement, "Arc", null);
				arcNode.setAttribute("FromNode", fromNode);
				arcNode.setAttribute("ToNode", toNode);
			}
			fromNode = toNode;
			HierarchyStep child;
			while (children.hasNext()) {
				child = children.next();
				fromNode = child.generateArcs(pParentElement, fromNode);
				toNode = getId() + "-" + (index++);
				arcNode = mOutput.addElement(pParentElement, "Arc", null);
				arcNode.setAttribute("FromNode", fromNode);
				arcNode.setAttribute("ToNode", toNode);
				fromNode = toNode;					
			}
			return fromNode;
		} else {
			if (fromNode != null) {
				arcNode = mOutput.addElement(pParentElement, "Arc", null);
				arcNode.setAttribute("FromNode", fromNode);
				arcNode.setAttribute("ToNode", getId());
			}
			return getId();
		}
	}
	
	//private String mNodeName;
}
